package com.example.administrator.testxingpiao.ui.main.exercise;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.administrator.testxingpiao.R;
import com.example.administrator.testxingpiao.base.BaseFragment;
import com.example.administrator.testxingpiao.bean.ExerciseBean;
import com.example.administrator.testxingpiao.bean.ListExerciseBean;
import com.example.administrator.testxingpiao.config.ConfigUrl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/8/4.
 */
public class ExerciseFragment extends BaseFragment implements ExerciseContract.View {

    @BindView(R.id.recyclerview_exercise)
    RecyclerView recyclerviewExercise;
    private ExerciseAdapter exerciseAdapter;
    private List<ListExerciseBean> list;
    private LinearLayoutManager layoutManager;
    private ExerciseContract.Presenter presenter;
    private ExerciseContract.Model model;
    private Handler handler=new Handler();
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View fragmentView = inflater.inflate(R.layout.fragment_exercise, container, false);
        ButterKnife.bind(this, fragmentView);
        //需要加载数据，就在ExerciseContact中的PresenterinitData(),
        model = new ExerciseModel();
        presenter = new ExercisePresenter(this, model);
        presenter.initData(initParams());
        list=new ArrayList<>();
        exerciseAdapter = new ExerciseAdapter(R.layout.item_fragment_exercise, list,getContext());
        layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        recyclerviewExercise.setLayoutManager(layoutManager);
        recyclerviewExercise.setAdapter(exerciseAdapter);

        return fragmentView;

    }

    public HashMap<String, String> initParams() {
        HashMap<String, String> params = new HashMap<>();
        params.put(ConfigUrl.Key.ACT, "activitylist");
        params.put(ConfigUrl.Key.REQ, "{\"v\":\"1.0\",\"o\":\"wap\",\"t\":1465978896117,\"an\"" +
                ":\"ixingmei\",\"av\":\"1.0.0\",\"ci\":\"289\",\"lu\":\"\",\"lt\":\"\",\"args\"" +
                ":{}}");
        return params;
    }

    @Override
    public void onSuccessGetExercise(final ExerciseBean exerciseListBeens) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                //拿到数据
                list.addAll(exerciseListBeens.getResult().getList());
                exerciseAdapter.notifyDataSetChanged();
            }
        });

    }
    @Override
    public void onFailGetExercise(String msg) {
        Toast.makeText(getContext(), "" + msg, Toast.LENGTH_SHORT).show();
    }
}
